package com.springboot;

import com.springboot.config.MyConfig;
import com.springboot.controller.MyServlet;
import com.springboot.controller.MyServlet2;
import com.springboot.filter.MyFilter;
import com.springboot.linstener.MyLinsener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.RegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableConfigurationProperties(MyConfig.class)
@EnableAsync
public class Start {

    public static void main(String[] args) {
        new MyApplication(Start.class, args);
    }




    @Bean
    public ServletListenerRegistrationBean<MyLinsener> myLinstener(){
        ServletListenerRegistrationBean<MyLinsener> servletListenerRegistrationBean = new ServletListenerRegistrationBean<>();
        MyLinsener linsener = new MyLinsener();
        servletListenerRegistrationBean.setListener(linsener);
        servletListenerRegistrationBean.setEnabled(true);
        return servletListenerRegistrationBean;
    }

    @Bean
    @ConditionalOnClass(RegistrationBean.class)
    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
    public ServletRegistrationBean myServlet(){
        MyServlet myServlet = new MyServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean<MyServlet>();
        registrationBean.setServlet(myServlet);
        registrationBean.setName("myServlet");
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/myservlet");
        return registrationBean;

    }

    @Bean("myServlet2")
    @ConditionalOnClass(RegistrationBean.class)
    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
    public ServletRegistrationBean myServlet2(){
        MyServlet2 myServlet = new MyServlet2();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean<MyServlet2>();
        registrationBean.setServlet(myServlet);
        registrationBean.setName("myServlet2");
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/myservlet2");
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean myFilter(){
        MyFilter myFilter = new MyFilter();
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(myFilter);
        filterRegistrationBean.setName("MyFilter");
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }


}
